package com.ourteam.product.api;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.product.ui.dao.ProductUITabDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUITabBean;
import com.ourteam.product.ui.domain.ProductUITabItemBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.model.tab.TabConfig;
import net.ui.model.tab.TabbedPaneConfig;

@Controller
@RequestMapping("/api/PRODUCT/UI/productUITab")
public class ProductUITabAPI
		extends AbstractCRUDServiceController<ProductUITabDAOQueryBean, ProductUITabSearchModel, ProductUITabBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUITabDAOQueryBean newQueryBean() {
		return new ProductUITabDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUITabDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUITabCount(queryBean);
	}

	@Override
	protected ProductUITabBean[] queryData(ProductUITabDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUITabs(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUITabs(ids);
	}

	@Override
	protected ProductUITabBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUITabById(id);
	}

	@Override
	protected void saveData(ProductUITabBean dataBean) throws Exception {
		dataBean.setLastModifyTime(new Date());
		dataBean.setLastModifyUser(getLoginUserName());
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUITab(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUITab(dataBean);
		}

	}

	@RequestMapping("/getTab")
	@ResponseBody
	public TabbedPaneConfig getTab(@RequestParam long id) throws Exception {

		ProductUITabBean productUITabBean = PRODUCT_UI_SERVICE.getProductUITabById(id);

		TabbedPaneConfig tabbedPaneConfig = new TabbedPaneConfig();

		tabbedPaneConfig.setId(String.valueOf(productUITabBean.getId()));

		tabbedPaneConfig.setDesc(productUITabBean.getRemarks());

		ProductUITabItemBean[] uiTabItemBeans = PRODUCT_UI_SERVICE.getProductUITabItems(productUITabBean.getId());

		for (int j = 0; j < uiTabItemBeans.length; j++) {

			ProductUITabItemBean productUITabItemBean = uiTabItemBeans[j];

			TabConfig tabConfig = new TabConfig();

			tabConfig.setLabel(productUITabItemBean.getTitle());

			tabConfig.setLabelKey(productUITabItemBean.getTitleKey());

			tabConfig.setIcon(productUITabItemBean.getIcon());

			tabConfig.setName(productUITabItemBean.getCode());

			tabConfig.setCompentclass(productUITabItemBean.getCompentClass());

			tabbedPaneConfig.addTab(tabConfig);
		}

		return tabbedPaneConfig;
	}

}
